//服务器地址
import {server} from '../config/index.js';

mui.init({
	gestureConfig: {
		tap: true, //默认为true
	    doubletap: true, //默认为false
	    longtap: true, //默认为false
	    swipe: true, //默认为true
	    drag: true, //默认为true
	    hold:true,//默认为false，不监听
	    release:true//默认为false，不监听
	}
});
castapp.init();

var my_userInfo = localStorage.getItem('userInfo');
my_userInfo = my_userInfo? JSON.parse(my_userInfo): {};
var my_qrcode = my_userInfo.qrcode;

var qrcodeEle = document.querySelector('#qrcode_box');
var form_divEle = document.querySelector('#form_div');
var inputELems = document.querySelectorAll('.mui-input-row input'); //输入框
var resetBtn = document.querySelector('header span'); //重置按钮
var confirmBtn = document.querySelector('#form_div form button');  //确定按钮 


if(my_qrcode){  //如果有保存过该二维码
	mui.plusReady(function(){
		//查看该二维码是否还存在
		var qrcode_addr = '_doc/imgs/qrcode_address.jpg';
		plus.io.resolveLocalFileSystemURL(qrcode_addr, function(entry){
			qrcode_addr = plus.io.convertAbsoluteFileSystem(qrcode_addr);
			qrcodeEle.style.display = 'block';
			form_divEle.style.display = 'none';
			qrcodeEle.innerHTML = `<img src='${qrcode_addr}' />`
			
		}, function(e){  //文件不存在
			qrcodeEle.style.display = 'none';
			form_divEle.style.display = 'block';
		});
	});
}else{
	qrcodeEle.style.display = 'none';
	form_divEle.style.display = 'block';
}

//生成通讯录二维码
function generater_qrcode(obj){
	new QRCode(document.getElementById("qrcode"), {
		text: `BEGIN:VCARD
		        FN:${obj.username}
		        ORG:${obj.company}
		        TITLE:${obj.job}
		        ADR;WORK:${obj.job_addr}
		        TEL;WORK:${obj.telephone}
		        EMAIL;WORK:${obj.email}
		        URL:${obj.my_page}
		        NOTE:${obj.remark}
		        END:VCARD`,
		width: 200,
		height: 200,
		colorDark : "#000000",
		colorLight : "#ffffff",
		correctLevel : QRCode.CorrectLevel.M  //容错级别
	});
};

//点击重置按钮
resetBtn.addEventListener('tap', function(e){
	e.stopPropagation();
	qrcodeEle.style.display = 'none';
	form_divEle.style.display = 'block';
});

//点击确定按钮
confirmBtn.addEventListener('tap', function(e){
	e.stopPropagation();
	var username = inputELems[0].value.trim();
	var company = inputELems[1].value.trim();
	var job = inputELems[2].value.trim();
	var job_addr = inputELems[3].value.trim();
	var telephone = inputELems[4].value.trim();
	var email = inputELems[5].value.trim();
	var my_page = inputELems[6].value.trim();
	var remark = inputELems[7].value.trim();
	
	if(!username && !company && !job && !job_addr && !telephone && !email && !my_page && !remark){
		castapp.prompt('您还没有填写信息');
		return;
	}
	
	var tmp_obj = {
		username, company, job, job_addr, telephone, email, my_page, remark
	}
	generater_qrcode(tmp_obj);
	
	qrcodeEle.style.display = 'block';
	form_divEle.style.display = 'none';
});


//长按图片保存
mui('#qrcode').on('longtap', 'img', function(e){
	e.stopPropagation();
	var src = this.src;
	
	//弹出选择按钮框
	mui.plusReady(function(){
		plus.nativeUI.actionSheet({
			buttons: [{title: '保存图片'}]
		}, function(e){
			var index = e.index;
			if(index ==1){  //点击了保存图片
				//首先判断该图片是否是base64位
				var isBase64 = src.indexOf('qrcode_address.jpg');
				if(isBase64== -1){  // 是base54
					var bitmap = new plus.nativeObj.Bitmap();
					//加载base64图片
					bitmap.loadBase64Data(src, function(){
						//加载图片成功
						var qrcode_addr = '_doc/imgs/qrcode_address.jpg';
						bitmap.save(qrcode_addr, {
							overwrite: true
						}, function(i){
							//转为绝对地址
							qrcode_addr = plus.io.convertAbsoluteFileSystem(qrcode_addr)
							castapp.prompt('保存成功:'+qrcode_addr);
							my_userInfo.qrcode = qrcode_addr;
							localStorage.setItem('userInfo', JSON.stringify(my_userInfo));
						}, function(e){
							castapp.prompt('保存失败');
						});
					}, function(e){
						castapp.prompt('保存失败:'+ JSON.stringify(e));
					});
				}else{
					var qrcode_addr = '_doc/imgs/qrcode_address.jpg';
					var bitmap = new plus.nativeObj.Bitmap();
					bitmap.save(qrcode_addr, {
						overwrite: true
					}, function(i){
						//转为绝对地址
						qrcode_addr = plus.io.convertAbsoluteFileSystem(qrcode_addr)
						castapp.prompt('保存成功:'+qrcode_addr);
						my_userInfo.qrcode = qrcode_addr;
						localStorage.setItem('userInfo', JSON.stringify(my_userInfo));
					}, function(e){
						castapp.prompt('保存失败');
					});
				}
			}
		})
	});
});